
	** Clean Niger Wave 2
	
	** Merge datasets
				
		use "$dir/rawdata/EHCVM Niger/2014/ECVMA2_MS01P1.dta", clear // roster
		
			*household id
			gen grappe_str=string(GRAPPE)
			replace grappe_str="00"+grappe_str if strlen(grappe_str)==1
			replace grappe_str="0"+grappe_str if strlen(grappe_str)==2
			gen menage_str=string(MENAGE)
			replace menage_str="0"+menage_str if strlen(menage_str)==1
			gen extension_str=string(EXTENSION)
			gen hhid=grappe_str+menage_str+extension_str // hhid for consistency with other countries
			
			*individuaal id
			gen indiv=MS01Q00 // renaming individual id to indiv for consistency with other countries
			
			* Check if unique id
			isid hhid indiv
			
			tempfile roster
			save `roster', replace
			
		use "$dir/rawdata/EHCVM Niger/2014/ECVMA2_MS00P1.dta", clear // sector (rural/urban)
		
			*household id
			gen grappe_str=string(GRAPPE)
			replace grappe_str="00"+grappe_str if strlen(grappe_str)==1
			replace grappe_str="0"+grappe_str if strlen(grappe_str)==2
			gen menage_str=string(MENAGE)
			replace menage_str="0"+menage_str if strlen(menage_str)==1
			gen extension_str=string(EXTENSION)
			gen hhid=grappe_str+menage_str+extension_str // hhid for consistency with other countries
			
			* Check if unique id
			isid hhid
			
			tempfile sector
			save `sector', replace
			
		use "$dir/rawdata/EHCVM Niger/2014/ECVMA2_MS04P1.dta", clear // employment
			
			*household id
			gen grappe_str=string(GRAPPE)
			replace grappe_str="00"+grappe_str if strlen(grappe_str)==1
			replace grappe_str="0"+grappe_str if strlen(grappe_str)==2
			gen menage_str=string(MENAGE)
			replace menage_str="0"+menage_str if strlen(menage_str)==1
			gen extension_str=string(EXTENSION)
			gen hhid=grappe_str+menage_str+extension_str // hhid for consistency with other countries
			
			*individuaal id
			gen indiv=MS04Q00
			
			* Check if unique id
			isid hhid indiv
			
			tempfile empl
			save `empl', replace
			
		use "$dir/rawdata/EHCVM Niger/2014/ECVMA2_MS03P1.dta", clear // health
			
			*household id
			gen grappe_str=string(GRAPPE)
			replace grappe_str="00"+grappe_str if strlen(grappe_str)==1
			replace grappe_str="0"+grappe_str if strlen(grappe_str)==2
			gen menage_str=string(MENAGE)
			replace menage_str="0"+menage_str if strlen(menage_str)==1
			gen extension_str=string(EXTENSION)
			gen hhid=grappe_str+menage_str+extension_str // hhid for consistency with other countries
			
			*individual id 
			gen indiv=MS03Q00
			
			* There are some missing individual IDs 
			bys hhid: gen miss = indiv == .  			// Identify missing individual IDs
			bys hhid: gen check = (_N == 1 & miss == 1)
			unique hhid if miss == 1
			unique hhid if miss == 1 & check == 1
				// 191 households have a missing ID, 189 HHs have only 1 individual
			
			* Check if unique id
			duplicates report hhid indiv
			
			tempfile health
			save `health', replace
			
			use `roster', clear
			merge m:1 hhid using `sector', gen(merge_sector) assert(matched) keepusing(MS00Q15)
			merge 1:1 hhid indiv using `empl', gen(merge_employment) assert(matched)
			merge 1:1 hhid indiv using `health', gen(merge_health)  // assert(matched)
				// 720 observations not found in master?
				unique hhid if merge_health == 2
				drop if merge_health==2
	
		***********
		***********	
	
		** Household size
			gen w2_inhh=MS01Q00F==1
			bys hhid: egen w2_hh_size=total(w2_inhh)
			
		** Marital status
			gen w2_married=(MS01Q15==2 | MS01Q15==3) if !missing(MS01Q15) & MS01Q15 != 9 // missing also coded as 9

		** Female
			gen w2_female=(MS01Q01==2) if !missing(MS01Q01)

		** Age
			gen w2_age=MS01Q06A // age on last birthday 
		
		** Urban status
			gen w2_urban=MS00Q15!=3 if !missing(MS00Q15)
			
		***********
		***********
		
		** Any work
		
			** In last 7 days, any work in agriculture
				gen w2_work_agHH=(MS04Q01==1) if !missing(MS04Q01)
				
			** In last 7 days, any work in some kind of trade or salaried employment (for household or otherwise)
				gen w2_work_bizHHother=(MS04Q02==1) if !missing(MS04Q02)
				
			** In last 7 days, any work outside the HH
				gen w2_work_outsideHH=(MS04Q03==1) if !missing(MS04Q03)
				
			** Apprenticeshops: MS04Q04: Not including
				
			** Any work in last 7 days (agriculture, for non-HH member, own account/business)
				gen w2_worked_last7days=0
				replace w2_worked_last7days=1 if MS04Q01==1
				replace w2_worked_last7days=1 if MS04Q02==1 
				replace w2_worked_last7days=1 if MS04Q03==1
				replace w2_worked_last7days=. if missing(MS04Q01) & missing(MS04Q02) & missing(MS04Q03)
					
		** Work hours
		
			** Usual months per year
				gen w2_worked_monthsperyear=MS04Q25
				
			** Usual weeks per month
				gen w2_worked_weekspermonth=MS04Q26
				
			** Usual days per month
				*gen w2_worked_dayspermonth=MS04Q27 * MS04Q26 if MS04Q27!=9 // this wave has days per week, multiply by weeks per month to get days per month
				*replace w2_worked_dayspermonth=0 if w2_worked_last7days==0 // not enforcing since about work over last 12 months not over last 7 days
			
			** Usual days per week
				gen w2_worked_daysperweek=MS04Q27 if MS04Q27!=9
				*replace w2_worked_daysperweek=0 if w2_worked_last7days==0 // not enforcing
			
			** Usual hours per day
				gen w2_worked_hoursperday=MS04Q28
				*replace w2_worked_hoursperday=0 if w2_worked_last7days==0 // not enforcing
				
			** Weekly hours
				gen w2_work_hrs=((w2_worked_monthsperyear/12) * (w2_worked_weekspermonth/4) * w2_worked_daysperweek * w2_worked_hoursperday) // interpretation is typical weekly hours
					// using weekspermonth/4 since options are 1, 2, 3, 4
				*gen w2_work_hrs=(w2_worked_dayspermonth*w2_worked_hoursperday)/(30/7)
				
		***********
		***********
		
		** Any illness or injury during the last 30 days
			gen w2_illinj=(MS03Q01==1) if !missing(MS03Q01) 

		** Had to stop usual activity in the past 4 weeks
			gen w2_illinj_stopactivity=(MS03Q03==1) if !missing(MS03Q03)
			replace w2_illinj_stopactivity=0 if w2_illinj==0
			
			
		***********
		***********
		
		** Depression
		
			** N/A
			
		***********
		***********
		
		** Functional limitations
		
			** N/A
			
				
		***********
		***********
		
		ren grappe w2_grappe 
		destring w2_grappe, replace
		
		keep hhid indiv w2*
		order hhid indiv w2*
		
		save "$dir/data/niger_w2_individual.dta", replace
